package control.sistoc;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import model.business.Curva;
import model.business.Item;
import model.business.ItemOc;
import model.business.LogicFacade;
import model.business.Orcamento;
import model.business.OrcamentoFrio;
import model.business.Ordem;
import model.quality.TestManager;

import org.springframework.util.Assert;

import control.ViewControl;

public class UIItemControl {
	
	private String result="error";
	private ViewControl vc;
	private Ordem oc;
	private Item item;
	private LogicFacade logicFacade;
	private String temp;
	private Boolean canSee=true;
	private String numero;
	private Integer ncurvas;
	private Integer pageNumber=1;
	
	private String txtNoc;
	private String txtCodigo;
	private Number txtDiametro;
	private Number txtQuantidade;
	private Number txtPreco;
	private String txtMaterial;
	private Number txtEspessura;
	private String optMoeda;

	

	public String getOptMoeda() {
		try {
			boolean moeda=item.getMoeda();
			if (moeda){
				optMoeda="1";
			}
			else{
				optMoeda="0";
			}
			return this.optMoeda;
			}
			catch (Exception e){
				this.solveError("");
				return "";
		}
	}
		

	
	public void setOptMoeda(String optMoeda) {
		try {
			if (optMoeda.equalsIgnoreCase("0")) {
				item.setMoeda(false);
			}
			else{
				item.setMoeda(true);				
			}
			this.optMoeda = optMoeda;
			}
			catch (Exception e){
				this.solveError("");
			}
		
		}
			
	
	public Integer getNcurvas() {
		try{
			return item.getCurvas().size();
		}
		catch (Exception e){
			return 1;
		}
	}

	public void setNcurvas(Integer ncurvas) {
		this.ncurvas = ncurvas;
	}

	public String getNumero() {
		return numero;
	}

	public void setNumero(String numero) {
		this.numero = numero;
	}

	public UIItemControl(){
		TestManager tm=TestManager.getInstance();
		vc=tm.getViewControl();		
		oc= vc.getOrdem();
		item=vc.getItem();		
		if (item==null){ canSee=false;}
	}	
	
	public String getResult() {
		return result;
	}


	public ViewControl getVc() {
		return vc;
	}


	public void setVc(ViewControl vc) {
		this.vc = vc;
	}


	public Ordem getOc() {
		return oc;
	}


	public void setOc(Ordem oc) {
		this.oc = oc;
	}


	public LogicFacade getLogicFacade() {
		return logicFacade;
	}


	public void setLogicFacade(LogicFacade logicFacade) {
		this.logicFacade = logicFacade;
	}


	public String getTemp() {
		return temp;
	}


	public void setTemp(String temp) {
		this.temp = temp;
	}

	public Boolean getCanSee() {
		return canSee;
	}


	public void setCanSee(Boolean canSee) {
		this.canSee = canSee;
	}
	

	public void setResult(String result) {
		this.result = result;
	}
	public void afterPropertiesSet() throws Exception {
		Assert.notNull(getLogicFacade(), "LogicFacade must be set");		
	}
	
	public String property2String(short p){
		try{
			Short u=p;
			return u.toString();
		}
		catch (Exception e){
			return "0";
		}			
	}
	
	
	
	public void doNew(ActionEvent event) {
			Item item = new Item();
			item.setNovo(true);
			vc.setItem(item);
			this.item=item;
			this.canSee=true;
	}
	
	public void doSave(ActionEvent event) {
		try {
			if (item.isNovo()){
				item.setOrdem(oc);
				oc.addItem(item);
				item.setNovo(false);
			}
			logicFacade.updateOrdem(oc);
		}
		catch (Exception e){
			e.printStackTrace();
			item.setNovo(false);
		}
	}
	
	public void doClose(ActionEvent event) {
		try {
			canSee=false;
			item=null;
			vc.setItem(null);
		}
		catch (Exception e){
			e.printStackTrace();
		}
	}	
	
	
	public void doOpen(ActionEvent event) {
		try {
			Item itemoc=oc.getItem().get(0);
			vc.setItem(itemoc);
			this.item=itemoc;
			this.canSee=false;
		}
		catch (Exception e){
			this.doNew(event);
		}
	}
	
	public void doSaveAs(ActionEvent event) {
		try {
			Item item=this.item.copy();
			oc.addItem(item);
			item.setNovo(false);
			logicFacade.updateOrdem(oc);
		}
		catch (Exception e){
			e.printStackTrace();
			item=null;
			this.canSee=false;
		}				
	}
	
	public void doExclude(ActionEvent event) {
		try {
			oc.removeItem(item);
			item.setNovo(false);
			logicFacade.updateOrdem(oc);
			this.item=oc.getItem().get(0);
		}
		catch (Exception e){
			e.printStackTrace();
			this.item=null;
			this.canSee=false;
		}				
	}

	public List getItemList(){
		List itens=oc.getItem();
		List itemList=new ArrayList();
		try{		
			Iterator i =itens.iterator();
			while (i.hasNext()){
				//I use the iterator to avoid errors.
				//the return list contains SelectItens to populate the combobox.
				Item inv=(Item) i.next();
				String id=inv.getNumero().toString();
				Integer numero=inv.getNumero()+1;
				String number=numero.toString();
				SelectItem sitem=new SelectItem();
				sitem.setValue(id); //The value of the item is the orcament ID
				sitem.setLabel(number);//the name is the name of the orcamento
				itemList.add(sitem);  
			}
			if (itemList.size()==0){
				SelectItem sitem=new SelectItem();
				sitem.setValue("-1"); //The value of the item is the orcament ID
				sitem.setLabel("Ordem sem itens.");//the name is the name of the orcamento				
				itemList.add(sitem);
			}
			return itemList;//I garantee the returning list exists and have the correct itens
		}
		catch (Exception e){
			itemList=new ArrayList();
			SelectItem sitem=new SelectItem();
			Integer size=oc.getItem().size()+1;
			sitem.setValue(size.toString());
			sitem.setLabel(size.toString());
			itemList.add(sitem);
			return itemList;
		}
	}
	public void selectItem(ActionEvent event) {
			
		FacesContext facesContext = FacesContext.getCurrentInstance();  
        Map map = facesContext.getExternalContext().getRequestParameterMap(); 
		String numero;
		Item item=null;
        if (map.get("itemId")==null){
			numero=this.getNumero();
			if (numero.equalsIgnoreCase("-1")) {item=null;}
			else{
			item=oc.getItem().get(Integer.parseInt(numero));}
        }
        else{
        	numero=(String) map.get("itemId");
        	Iterator iterator=oc.getItem().iterator();
        	while (iterator.hasNext()){
        		item = (Item) iterator.next();
        		if (item.getId()==Integer.parseInt(numero)) break;
        	}
        }			
			
			if (item==null){
				result="Nenhum item selecionado";//pos - no inventory was returned, meaning the company dont exist
				System.out.println("Error - Item selecionado nao existe");			
			}
			else{
				result="success";
				vc.setItem(item);//pos condition in case of success.
				this.item=item;
				this.canSee=true;
			}
	}
	
	public void updateCurves(javax.faces.event.ActionEvent event) {
		Integer ncurvas=this.ncurvas;
		Integer sizecurvas=this.item.getCurvas().size();
		if ((ncurvas-sizecurvas)>0){
			int i=ncurvas-sizecurvas;
			while (i>0){
				Curva c = new Curva();
				item.addCurva(c);				
				i--;
			} 
		}
		else{
			int i=sizecurvas-ncurvas;
			while (i>0){
				item.getCurvas().remove(item.getCurvas().size()-1);				
				i--;
			}			
		}
		this.item.setNumeroCurvas(ncurvas.shortValue());
	}
	
	
	
	
	
	public String getTxtNoc() {
		try {
			txtNoc=oc.getNumero();
			return txtNoc;
			}
			catch (Exception e){
				this.solveError("");
				return "";
			}
	}



	public void setTxtNoc(String txtNoc) {	}

	public String getTxtCodigo() {
		try {
			txtCodigo=item.getCodigo();
			return txtCodigo;
			}
			catch (Exception e){
				this.solveError("");
				return "";
			}
	}

	public void setTxtCodigo(String txtCodigo) {
		try {
			item.setCodigo(txtCodigo);
			this.txtCodigo = txtCodigo;
		}
		catch (Exception e){
			this.solveError("");			
		}
	}

	public Number getTxtDiametro() {
		try {
			txtDiametro=item.getDiametro();
			return txtDiametro;
			}
			catch (Exception e){
				this.solveError("");
				return 0;
			}
	}

	public void setTxtDiametro(Number txtDiametro) {
		try {
			item.setDiametro(txtDiametro.floatValue());
			this.txtDiametro = txtDiametro;
		}
		catch (Exception e){
			this.solveError("");			
		}	
	}

	public Number getTxtQuantidade() {
		try {
			txtQuantidade=item.getQuantidade();
			return txtQuantidade;
			}
			catch (Exception e){
				this.solveError("");
				return 0;
			}
	}

	public void setTxtQuantidade(Number txtQuantidade) {
		try {
			item.setQuantidade(txtQuantidade.intValue());
			this.txtQuantidade = txtQuantidade;
		}
		catch (Exception e){
			this.solveError("");			
		}	
	}

	public Number getTxtPreco() {
		try {
			txtPreco=item.getPrecoUnitario();
			return txtPreco;
			}
			catch (Exception e){
				this.solveError("");
				return 0;
			}
	}

	public void setTxtPreco(Number txtPreco) {
		try {
			item.setPrecoUnitario(txtPreco.floatValue());
			this.txtPreco = txtPreco;
		}
		catch (Exception e){
			this.solveError("");			
		}	
	}

	public String getTxtMaterial() {
		try {
			txtMaterial=item.getMaterial();
			return txtMaterial;
			}
			catch (Exception e){
				this.solveError("");
				return "";
			}
	}

	public void setTxtMaterial(String txtMaterial) {
		try {
			item.setMaterial(txtMaterial);
			this.txtMaterial = txtMaterial;
		}
		catch (Exception e){
			this.solveError("");			
		}
	}

	public Number getTxtEspessura() {
		try {
			txtEspessura=item.getEspessura();
			return txtEspessura;
			}
			catch (Exception e){
				this.solveError("");
				return 0;
			}
	}

	public void setTxtEspessura(Number txtEspessura) {
		try {
			item.setEspessura(txtEspessura.floatValue());
			this.txtEspessura = txtEspessura;
		}
		catch (Exception e){
			this.solveError("");			
		}	
	}



	public Item getItem() {
		return item;
	}

	public void setItem(Item item) {
		this.item = item;
	}

	
	
	
	public Integer getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(Integer pageNumber) {
		this.pageNumber = pageNumber;
	}

	private void solveError(String string) {
		// TODO Auto-generated method stub
		
	}
	
	
	
}
